package cn.kgc.jdbc.service;

import cn.kgc.jdbc.dao.*;
import cn.kgc.jdbc.entity.Course;
import cn.kgc.jdbc.entity.Student;
import cn.kgc.jdbc.utils.CodeMsg;
import cn.kgc.jdbc.utils.Result;
import cn.kgc.jdbc.vo.ScoreVO;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/8/29
 */
public class ScoreService {

    private StudentDao studentDao = new StudentDaoImpl();

    private CourseDao courseDao = new CourseDaoImpl();

    private ScoreDao scoreDao = new ScoreDaoImpl();

    //添加学生成绩
    public Result addScore(String stuName,String courseName,Integer score){
        Result result = new Result();
        //1.根据学生的名字查询学生id
        Student student = studentDao.findByName(stuName);
        Integer stuId = student.getId();
        // 2.根据科目的名字查询科目id
        Course course = courseDao.findByName(courseName);
        Integer courseId = course.getId();
        // 添加学生成绩
        Integer line = scoreDao.addScore(stuId, courseId, score);
        if (line>0){
            result.setCode(CodeMsg.SUCCESS.getCode());
            result.setMsg(CodeMsg.SUCCESS.getMsg());
        }else {
            result.setCode(CodeMsg.ERROR.getCode());
            result.setMsg(CodeMsg.ERROR.getMsg());
        }
        return  result;
    }

    // 根据科目名称查询所有学生的成绩，并降序排列
    public Result findScoreByCourseName(String courseName){

        Result result = new Result();
        //1.根据科目名字 查询科目id  score表中只有科目id
        Course course = courseDao.findByName(courseName);
        Integer courseId = course.getId();

        List<ScoreVO>  scoreVOS = scoreDao.findScoreByCourseId(courseId);

        result.setCode(CodeMsg.SUCCESS.getCode());
        result.setMsg(CodeMsg.SUCCESS.getMsg());
        result.setData(scoreVOS);

        return result;
    }




}
